package cn.iocoder.yudao.module.biz.controller.admin.specialWorkType;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.biz.controller.admin.specialWorkType.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.specialWorkType.BizSpecialWorkTypeDO;
import cn.iocoder.yudao.module.biz.convert.specialWorkType.BizSpecialWorkTypeConvert;
import cn.iocoder.yudao.module.biz.service.specialWorkType.BizSpecialWorkTypeService;

@Api(tags = "管理后台 - 工种类型")
@RestController
@RequestMapping("/biz/special-work-type")
@Validated
public class BizSpecialWorkTypeController {

    @Resource
    private BizSpecialWorkTypeService specialWorkTypeService;

    @PostMapping("/create")
    @ApiOperation("创建工种类型")
    @PreAuthorize("@ss.hasPermission('biz:special-work-type:create')")
    public CommonResult<String> createSpecialWorkType(@Valid @RequestBody BizSpecialWorkTypeCreateReqVO createReqVO) {
        return success(specialWorkTypeService.createSpecialWorkType(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新工种类型")
    @PreAuthorize("@ss.hasPermission('biz:special-work-type:update')")
    public CommonResult<Boolean> updateSpecialWorkType(@Valid @RequestBody BizSpecialWorkTypeUpdateReqVO updateReqVO) {
        specialWorkTypeService.updateSpecialWorkType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除工种类型")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:special-work-type:delete')")
    public CommonResult<Boolean> deleteSpecialWorkType(@RequestParam("id") String id) {
        specialWorkTypeService.deleteSpecialWorkType(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得工种类型")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:special-work-type:query')")
    public CommonResult<BizSpecialWorkTypeRespVO> getSpecialWorkType(@RequestParam("id") String id) {
        BizSpecialWorkTypeDO specialWorkType = specialWorkTypeService.getSpecialWorkType(id);
        return success(BizSpecialWorkTypeConvert.INSTANCE.convert(specialWorkType));
    }

    @GetMapping("/list")
    @ApiOperation("获得工种类型列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:special-work-type:query')")
    public CommonResult<List<BizSpecialWorkTypeRespVO>> getSpecialWorkTypeList(@RequestParam("ids") Collection<String> ids) {
        List<BizSpecialWorkTypeDO> list = specialWorkTypeService.getSpecialWorkTypeList(ids);
        return success(BizSpecialWorkTypeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/listAll")
    @ApiOperation("获得全部工种类型列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:special-work-type:query')")
    public CommonResult<List<BizSpecialWorkTypeRespVO>> getSpecialWorkTypeListAll() {
        List<BizSpecialWorkTypeDO> list = specialWorkTypeService.getSpecialWorkTypeListAll();
        return success(BizSpecialWorkTypeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得工种类型分页")
    @PreAuthorize("@ss.hasPermission('biz:special-work-type:query')")
    public CommonResult<PageResult<BizSpecialWorkTypeRespVO>> getSpecialWorkTypePage(@Valid BizSpecialWorkTypePageReqVO pageVO) {
        PageResult<BizSpecialWorkTypeDO> pageResult = specialWorkTypeService.getSpecialWorkTypePage(pageVO);
        return success(BizSpecialWorkTypeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出工种类型 Excel")
    @PreAuthorize("@ss.hasPermission('biz:special-work-type:export')")
    @OperateLog(type = EXPORT)
    public void exportSpecialWorkTypeExcel(@Valid BizSpecialWorkTypeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BizSpecialWorkTypeDO> list = specialWorkTypeService.getSpecialWorkTypeList(exportReqVO);
        // 导出 Excel
        List<BizSpecialWorkTypeExcelVO> datas = BizSpecialWorkTypeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "工种类型.xls", "数据", BizSpecialWorkTypeExcelVO.class, datas);
    }

}
